home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1993-94, Silicon Graphics, Inc.
- * ALL RIGHTS RESERVED
- *
- * Permission to use, copy, modify, and distribute this software for
- * any purpose and without fee is hereby granted, provided that the above
- * copyright notice appear in all copies and that both the copyright notice
- * and this permission notice appear in supporting documentation, and that
- * the name of Silicon Graphics, Inc. not be used in advertising
- * or publicity pertaining to distribution of the software without specific,
- * written prior permission.
- *
- * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
- * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
- * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
- * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
- * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
- * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
- * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
- * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
- * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
- * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
- * (c)(1)(ii) of the Rights in Technical Data and Computer Software
- * clause at DFARS 252.227-7013 and/or in similar or successor
- * clauses in the FAR or the DOD or NASA FAR Supplement.
- * Unpublished-- rights reserved under the copyright laws of the
- * United States. Contractor/manufacturer is Silicon Graphics,
- * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
- *
- * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int attributes[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- int width = 200, height = 200;
-
- static void DoDisplay(void)
- {
- static GLfloat red[] = { 1, 0, 0, 1 };
- static GLfloat green[] = { 0, 1, 0, 1 };
- static GLfloat black[] = { 0, 0, 0, 1 };
-
- /* Init two sided lighting */
- glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
- glMaterialfv(GL_FRONT, GL_EMISSION, red);
- glMaterialfv(GL_BACK, GL_EMISSION, green);
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, black);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-1, 1, -1, 1, 0, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glViewport(0, 0, width, height);
-
- /* Draw quad strip */
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_QUAD_STRIP);
- glVertex2f(-0.5, 0.5);
- glVertex2f(-0.5, -0.5);
- glVertex2f( 0.5, 0.5);
- glVertex2f( 0.5, -0.5);
- glVertex2f( 0.0, 0.5);
- glVertex2f( 0.0, -0.5);
- glEnd();
- glFlush();
-
- {
- GLfloat buf[10][4];
- GLint i;
-
- /* Read back some of the pixels from the middle of the window */
- glReadPixels(width/2 - 5, height/2, 10, 1, GL_RGBA, GL_FLOAT, buf);
- printf("Pixels @ %d,%d:\n", width/2-5, height/2);
- for (i = 0; i < 10; i++) {
- printf("%g %g %g %g\n",
- buf[i][0], buf[i][1], buf[i][2], buf[i][3]);
- }
- }
- }
-
- static void Usage(void)
- {
- printf("Usage: tvorder [-u]\n");
- printf(" -u: Render to unmapped window\n");
- exit(-1);
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- static Bool WaitForUnmapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == UnmapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(long argc, char** argv)
- {
- XVisualInfo *vi;
- Display *dpy;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- GLboolean unmapped = GL_FALSE;
- int i;
-
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'u':
- unmapped = GL_TRUE;
- break;
- default:
- Usage();
- }
- } else {
- Usage();
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocNone);
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- width, height,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetStandardProperties(dpy, window, "tvorder", "tvorder", None,
- argv, argc, NULL);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- if (unmapped) {
- DoDisplay();
- XUnmapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForUnmapNotify, (char*)window);
- DoDisplay();
- return 0;
- }
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- width = event.xconfigure.width;
- height = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_p:
- case XK_P:
- glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
- needDisplay = GL_TRUE;
- break;
- case XK_l:
- case XK_L:
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- needDisplay = GL_TRUE;
- break;
- case XK_f:
- case XK_F:
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay();
- }
- }
- }
-